/**
 * 初始化简历下载专题数据
 */
define(function(){
    var ec=require('echarts');
    require('echarts/chart/line');
    require('echarts/chart/funnel');
    require('echarts/chart/pie');
    
    var ecConfig = require('echarts/config');
    var zrUtil = require('zrender/tool/util');
    var zrnumber = require('echarts/util/number');

    //饼图的数据模版
	var option_pie_one= {
		title : {
	    	text: '',
	    	x:'center',
	    	y:'45'
	    },
	    tooltip : {
	        trigger: 'item',
	        formatter: "{a} <br/>{b} : {c} ({d}%)"
	    },
	    legend: {
	        x : 'left',
	        data:[]
	    },
	    calculable : true,
	    toolbox: {
                show : true,
                orient:'vertical',
                y:'bottom',
                feature : {
                    mark : {show: false},
                    dataView : {show: true, readOnly: false},
                    magicType : {
                        show: true, 
                        type: ['pie'],
                        option: {
                            funnel: {
                                x: '25%',
                                width: '50%',
                                funnelAlign: 'left',
                                max: 1548
                            }
                        }
                    },
                    restore : {show: true},
                    saveAsImage : {show: true}
                }
            },
	    series : [
	        {
	            name:'',
	            type:'pie',
	            radius : '50%',
	            center: ['50%', 200],
	            data:[]
	        }
	    ]
	};

//带timeline的饼图模版   
	var option_pie = {
	    timeline : {
		    type:"number",
		    height:40,
		    symbolSize:3,
		    x:"1%",
		    x2:"1%",
		    width:"100%",
		    label:{
		                interval: 'auto',
		                formatter:function(param){
		    				return param;
		    	        }
		        },
		    data : ['']
	    }, 
	    options:[]
	};
    
    var myChart_pie_source = ec.init(document.getElementById('down_pie_source'));
    var myChart_pie_fill = ec.init(document.getElementById('down_pie_fill'));
    var myChart_pie_transform = ec.init(document.getElementById('down_pie_transform'));
   
    var option_source=zrUtil.clone(option_pie);
//    option_source.series[0].name="来源";  
    var option_fill=zrUtil.clone(option_pie);
//    option_fill.series[0].name="填充";
    var option_transform=zrUtil.clone(option_pie);
//   option_transform.series[0].name="转化";
    
	var optionMap={"source":[myChart_pie_source,option_source], "transform": [myChart_pie_transform,option_transform],"fill": [myChart_pie_fill,option_fill]};
   
	function showPie(){
		
		var city_option = $('#city_down_option').val();
		var cate_option = $('#cate_down_option').val();				
		
		 $.each(optionMap,function(name,obj){
		 	
		 	var charts = obj[0];
            var option = obj[1];
            charts.showLoading({
                text: '正在努力的读取数据中...'
            });
	
	        $.ajax({
	        	url: '/dashboard/resume/ajax/getDownSubject',
	        	type: 'POST',
	       		async: true,
	        	data:{"city_option":city_option,"cate_option":cate_option,"index_Type":name},//根据name判断查哪个饼的数据
	        	dataType: 'json',
	         	success: function(data, textStatus) {
	
	                 if(name=="source")
	                 {
	                 	option.timeline.show=false;
	                 }
	                 else if(name=="transform")
	                 {
	                 	option.timeline.show=false;
	                 }
	                 		
	                 //设置饼图
	                 setPie(charts,option,data,name);
	                                     
	             },
	             timeout: 5000,
	             error: function() {
	                  charts.hideLoading();
	             }
	          });        
		  });
}

//设置饼图数据
	function setPie(echarts, option, initalData,name){
    	option.options=[];
    	//饼图时间轴
        option.timeline.data=initalData.timeline;
        option.timeline.currentIndex=initalData.timeline.length-1;   
        
        //饼图数据
        $.each(initalData.options,function(index,value){
        	var op= zrUtil.clone(option_pie_one);
          
        	op.series[0].name=index;
            op.series[0].data = value; 
            
            op.legend.data = initalData.cate;
            
            if(name=="source")
            {
            	op.toolbox.show=false;
            	op.title.text='列表页简历填充分布';
            }
	        else if(name=="fill")
	        {
	            op.toolbox.show=false;
	            op.title.text='简历点击获得分布';
	        }
	        else
	        {
	            op.title.text='简历下载效果分布';
	        }
                 
            option.options.push(op);
         });
         
         echarts.setOption(option,true);
         echarts.hideLoading();

	}
	
	 //timeline联动
	 myChart_pie_source.connect([myChart_pie_fill, myChart_pie_transform]);
	 myChart_pie_fill.connect([myChart_pie_source, myChart_pie_transform]);
	 myChart_pie_transform.connect([myChart_pie_source, myChart_pie_fill]);
	 

     myChart_pie_fill.on("timelineChanged", function(param){
     	myChart_pie_source.component.timeline.play(param.currentIndex,false);
     	myChart_pie_transform.component.timeline.play(param.currentIndex,false);
     	
     });
              
	

    $("#refresh_down_pie_btn").click(function(){
            showPie();
        });
 
        
     var EC={
            showPie:showPie,
            resize:function(){myChart_pie_source.resize(),myChart_pie_fill.resize(),myChart_pie_transform.resize()}                
        };
      
        return EC;
});